package com.msgserver.mbeans;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.primefaces.event.FileUploadEvent;

import com.msgserver.entity.Company;
import com.msgserver.entity.EmployeeGroup;
import com.msgserver.utils.MsgServerUtils;

@ManagedBean
@ViewScoped
public class MsgSenderBean {
	
	public MsgSenderBean(){
		
	}

	@ManagedProperty(value = "#{globalBean}")
	private GlobalBean gBean;
	
	private String text;
	private Company company;
	private List<SelectItem> groupItems = new ArrayList<SelectItem>();
	private String inputNumber;
	private List<String> uploadedList  = new ArrayList<String>();
	
	private String phoneNumbers = "";
	private Map<String, List<String>> uploadedNumbers = new HashMap<String, List<String>>();
	private List<String> uploadedTitles = new ArrayList<String>();
	
	@PostConstruct
	public void init(){
		getGroupItems().clear();
		if(getCompany() == null){
			company = gBean.getAdmin().getCompany();
		}
		for(EmployeeGroup group :company.getGroups()){
			SelectItem item = new SelectItem();
			item.setLabel(group.getName());
			item.setValue(group.getId());
			getGroupItems().add(item);
		}
	}
	
	public void sendMsg(){
		Integer x_gate_id = 300;
		String x_uid = company.getContract().getX_uid();
		String x_eid = company.getContract().getX_eid();
		String x_pwd_md5 = MsgServerUtils.getMD5(company.getContract().getX_pwd());
		String text = getText();
		String allNumbers = getAllNumbers();
		/*try {
			String result = MsgServerUtils.SendSms(allNumbers, text, x_eid, x_uid, x_pwd_md5, x_gate_id);
			System.out.println(result);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
	}
	
	private String getAllNumbers(){
		String numbers = "";
		for(Map.Entry<String, List<String>> entry:uploadedNumbers.entrySet()){
			List<String> list = entry.getValue();
			for (int i = 0; i < list.size(); i++) {
				numbers+=list.get(i)+",";
			}
		}
		String inputtedNumbers = getInputNumber();
		if(inputtedNumbers!=null && inputtedNumbers.trim() !=""){
			String[] numbersArray = inputtedNumbers.split("\n");
			for (int i = 0; i < numbersArray.length; i++) {
				String num = numbersArray[i];
				if(num.length() == 11 && MsgServerUtils.isNum(num)){
					numbers+=num+",";
				}
			}
		}
		return numbers;
	}
	
	public void uploadContactsFileListener(FileUploadEvent e){
		String fileName = e.getFile().getFileName();
		System.out.println(e.getFile().getSize());
		String fileExtension = fileName.substring(fileName.indexOf(".")+1);
		if(!fileExtension.equalsIgnoreCase("xls") && !fileExtension.equalsIgnoreCase("txt")){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"文件格式错误！",null));
		}
		//if the file is a txt file
		if(fileExtension.equalsIgnoreCase("txt")){
			try {
				List<String> uploadedEmps = MsgServerUtils.readText(e.getFile());
		
				String key = "uploaded a "+fileExtension.toUpperCase()+" containing "+uploadedEmps.size()+" of contacts";
				uploadedNumbers.put(key, uploadedEmps);
				
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}else if(fileExtension.equalsIgnoreCase("xls")){
			
		}
	}
	


	public GlobalBean getgBean() {
		return gBean;
	}


	public void setgBean(GlobalBean gBean) {
		this.gBean = gBean;
	}


	public String getText() {
		return text;
	}


	public void setText(String text) {
		this.text = text;
	}


	public List<SelectItem> getGroupItems() {
		return groupItems;
	}


	public void setGroupItems(List<SelectItem> groupItems) {
		this.groupItems = groupItems;
	}


	public Company getCompany() {
		return company;
	}


	public void setCompany(Company company) {
		this.company = company;
	}

	public String getInputNumber() {
		return inputNumber;
	}

	public void setInputNumber(String inputNumber) {
		this.inputNumber = inputNumber;
	}

	public String getPhoneNumbers() {
		return phoneNumbers;
	}

	public void setPhoneNumbers(String phoneNumbers) {
		this.phoneNumbers = phoneNumbers;
	}

	public List<String> getUploadedList() {
		return uploadedList;
	}

	public void setUploadedList(List<String> uploadedList) {
		this.uploadedList = uploadedList;
	}

	public List<String> getUploadedTitles() {
		uploadedTitles.clear();
		for(Map.Entry entry:uploadedNumbers.entrySet()){
			uploadedTitles.add(entry.getKey().toString());
		}
		return uploadedTitles;
	}

	public void setUploadedTitles(List<String> uploadedTitles) {
		this.uploadedTitles = uploadedTitles;
	}
	 
	
	
	
}
;